package com.bird.thread.base.uncaughtexception;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author sijunqiang
 * @data 2021/7/3 14:59
 * @description: 功能：实现自己uncaughtExceptionHandle 进行捕获 未捕获的异常
 */
public class MyUncaughtExceptionHandle implements Thread.UncaughtExceptionHandler {
    // 用来记录哪个线程的名称
    private String name;

    public MyUncaughtExceptionHandle(String name) {
        this.name = name;
    }


    /**
     * 处理未捕获的异常
     *
     * @param t 哪个线程
     * @param e 异常的信息
     */
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        Logger logger = Logger.getAnonymousLogger();
        logger.log(Level.WARNING, "线程异常,终止了线程 " + t.getName(), e);
        System.out.println(name + " 捕获了异常 " + t.getName() + " 异常 " + e);

//        if ("thread-4".equals(t.getName())) {
//            System.out.println("捕获到这个线程 " + t.getName() + "抛出的异常" + "异常的信息为 " + e);
//        }
    }
}
